package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /*添加购物车接口*/
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart Cart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        BeanUtils.copyProperties(shoppingCartDTO, Cart);
        //1.首先查询购物车看商品是否存在   SelectCart:条件查询购物车中的商品
        List<ShoppingCart> list = shoppingCartMapper.SelectCart(Cart);
        //2.如果存在 商品数量加1
        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart = list.get(0); //获得查询到的购物车数据
            shoppingCart.setNumber(shoppingCart.getNumber() + 1); //数量加1
            shoppingCartMapper.setGoodsNumberById(shoppingCart);
        } else {
            //3.如果不存在 则分类判断之后 新增一条商品数据
            if (Cart.getSetmealId() != null) {
                //本次添加的是套餐商品 查询套餐表添加
                Setmeal setmeal = setmealMapper.getSetMealById(Cart.getSetmealId());
                Cart.setAmount(setmeal.getPrice());
                Cart.setImage(setmeal.getImage());
                Cart.setName(setmeal.getName());
            } else {
                //本次添加的是普通菜品 查询菜品表添加
                Dish dish = dishMapper.getById(Cart.getDishId());
                Cart.setAmount(dish.getPrice());
                Cart.setImage(dish.getImage());
                Cart.setName(dish.getName());
            }
            Cart.setNumber(1);
            Cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addCart(Cart);
        }
    }


    /*查看购物车接口*/
    public List<ShoppingCart> listCart() {
        List<ShoppingCart> list = shoppingCartMapper.list();
        return list;
    }

    /*根据用户id清空购物车*/
    public void cleanCart(Long userId) {
        shoppingCartMapper.clean(userId);
    }

    /*删除购物车某一个商品*/
    public void deleteCartGoods(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //1.删除时 首先查询当前操作的商品的数量
        List<ShoppingCart> list = shoppingCartMapper.SelectCart(shoppingCart);
        ShoppingCart Cart = list.get(0);
        //2.如果数量为1 直接删除该条商品数据
        //3.如果不为1 则更新商品 使数量减一
        if (Cart.getNumber() == 1) {
            shoppingCartMapper.deleteCartGoods(Cart);
        } else {
            Cart.setNumber(Cart.getNumber()-1);
            shoppingCartMapper.setGoodsNumberById(Cart);
        }
    }
}
